package com.example.a22310.myapplication.Activity;
//红色的R解决不了
import android.Manifest;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.service.autofill.Transformation;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Beans.Choiceness;
import com.example.a22310.myapplication.Dailog.ActivityDialog;
import com.example.a22310.myapplication.R;
import com.example.a22310.myapplication.databinding.ActivityDetailBinding;
import com.squareup.picasso.Picasso;

public class ActivityDetail extends AppCompatActivity {

    ActivityDetailBinding binding;

    Transformation transformation = new transformation() {    //图片等比缩放

        @Override
        public Bitmap transform(Bitmap source) {

            int targetWidth = binding.activityPicture.getWidth();
            //   LogCat.i("source.getHeight()="+source.getHeight()+",source.getWidth()="+source.getWidth()+",targetWidth="+targetWidth);

            if(source.getWidth()==0){
                return source;
            }

            //如果图片小于设置的宽度，则返回原图
            if(source.getWidth()<targetWidth){
                return source;
            }else{
                //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                if (targetHeight != 0 && targetWidth != 0) {
                    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                    if (result != source) {
                        // Same bitmap is returned if sizes are the same
                        source.recycle();
                    }
                    return result;
                } else {
                    return source;
                }
            }

        }

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };
    private Choiceness.ActivitydataBean choiceness;
    private String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_detail);
        Intent intent = getIntent();
        choiceness = (Choiceness.ActivitydataBean) intent.getSerializableExtra("Choiceness");
        str = choiceness.getActivity_picture().substring(52, choiceness.getActivity_picture().length());
        Picasso.with(this).load(str).transform(transformation).into(binding.activityPicture);
        binding.activityLovenum.setText(choiceness.getActivity_lovenum()+"");
        binding.activityPeoplenum.setText(choiceness.getActivity_peoplenum()+"");
        binding.activityFree.setText(choiceness.getActivity_free()+"");
        binding.phone.setText(choiceness.getActivity_telephone()+"");
        binding.setDetail(choiceness);
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
                finish();
            }
        });
        binding.trianRequsetion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                consultOnlieDialog();
            }
        });


        binding.trianApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(ActivityDetail.this,TrainApply.class);
                Bundle bundle = new Bundle();
                bundle.putInt("activityid",choiceness.getActivity_id());
                bundle.putString("name",choiceness.getActivity_name());
                bundle.putString("price",choiceness.getActivity_free()+"");
                bundle.putString("str",str);
                bundle.putString("intro",choiceness.getActivity_intro());
                intent1.putExtras(bundle);
                startActivity(intent1);

            }
        });


    }





    public void consultOnlieDialog() {
        FragmentManager fm = getFragmentManager();
        ActivityDialog dialog = new ActivityDialog(binding.phone.getText().toString());
        dialog.show(fm, "showPhotoSelDialog");
    }



    public void callPhone(){

        //1. 检查用户是否已经给我们授权了权限    相等则已经授权,不等则没授权
        if (ContextCompat.checkSelfPermission(ActivityDetail.this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //2. 申请权限
            //参数:Context上下文,权限数组,申请码(申请码只要唯一就行)
            ActivityCompat.requestPermissions(ActivityDetail.this,
                    new String[]{Manifest.permission.CALL_PHONE}, 1);
        } else {
            call();
        }

    }




    /**
     * 打电话
     */
    private void call() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+binding.phone.getText().toString()));
            startActivity(intent);
        } catch (SecurityException e){   //这里要写SecurityException不然上面startActivity()会报红
            e.printStackTrace();
        }
    }

    //3. 判断是否申请成功 申请不管成功与否都会调用此方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                //用户授权的结果会封装在grantResults中
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //申请成功
                    call();
                } else {

                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}
